<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png"
             alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="goOut">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="iscollapse ? '64px' : '200px'">
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409eff"
                 unique-opened
                 :collapse='iscollapse'
                 :collapse-transition='false'
                 :router='true'
                 :default-active='activePath'>
          <div class="toggle-button"
               @click="toggle">|||</div>
          <!-- 一级菜单 -->
          <!-- 这里注意：绑定index属性时 后面的值必须是字符串 而item.id是数值 需要转换为字符串 -->
          <el-submenu :index="item.id + ''"
                      v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- '/' + subItem.path 通过:router="true" -->
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="navState('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{subItem.authName}}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <!-- welcome的路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data () {
    return {
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },

      // 获取左侧菜单
      menuList: [],
      iscollapse: false,
      activePath: ''
    }
  },
  created () {
    this.getMenusList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    navState (active) {
      window.sessionStorage.setItem('activePath', active)
      this.activePath = active
    },
    // 点击切换收缩左侧列表
    toggle () {
      this.iscollapse = !this.iscollapse
    },

    // 发送ajax获取左侧菜单
    async getMenusList () {
      const { data: res } = await this.$http.get('/menus')
      // console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 点击退出

    goOut () {
      // 删除token
      window.sessionStorage.clear()
      // 跳转到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 18px;
  > div {
    display: flex;
    align-items: center;
  }
  span {
    margin-left: 20px;
  }
}
.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle-button {
  color: #fff;
  text-align: center;
  background-color: skyblue;
  line-height: 24px;
  font-size: 14px;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
